package domain.factory;

import java.util.Set;

import domain.Address;
import domain.Contact;
import domain.ContactGroup;
import domain.Entreprise;
import domain.PhoneNumber;


public class POJOFactory {

	public static POJOFactory instance = new POJOFactory();
	
	private POJOFactory() { super(); }
	
	public Address newAddress(String street, String city, String zip, String country) {
		Address address = new Address();
		address.setStreet(street);
		address.setCity(city);
		address.setZip(zip);
		address.setCountry(country);
		return address;
	}
	
	public Contact newContact(String firstname, String lastname,
			String emailC, Address address, Set<ContactGroup> books, Set<PhoneNumber> phones) {
		Contact contact = new Contact();
		contact.setFirstName(firstname);
		contact.setLastName(lastname);
		contact.setEmail(emailC);
		contact.setAddress(address);
		
		if (books != null) {
			contact.setBooks(books);
			// warning, the bidirectionnal link have to be set manually (lazy problem)
//			for (ContactGroup contactGroup : books)
//				contactGroup.getContacts().add(contact);
		}
		
		if (phones != null) {
			contact.setPhones(phones);
			for (PhoneNumber phoneNumber : phones) 
				phoneNumber.setContact(contact);
		}
		
		return contact;
	}
	
	public ContactGroup newContactGroup(String name, Set<Contact> contacts) {
		ContactGroup group = new ContactGroup();
		group.setGroupName(name);
		group.setContacts(contacts);
		
		if (contacts != null)
			for (Contact contact : contacts)
				contact.getBooks().add(group);
		
		return group;
	}
	
	public Entreprise newEntreprise(String firstname, String lastname,
			String emailC, Address address, Set<ContactGroup> books, Set<PhoneNumber> phones, int numSiret) {
		Entreprise entreprise = new Entreprise();
		entreprise.setFirstName(firstname);
		entreprise.setLastName(lastname);
		entreprise.setEmail(emailC);
		entreprise.setAddress(address);
		
		if (books != null) {
			entreprise.setBooks(books);
			// warning, the bidirectionnal link have to be set manually (lazy problem)
//			for (ContactGroup contactGroup : books)
//				contactGroup.getContacts().add(entreprise);
		}
		
		if (phones != null) {
			entreprise.setPhones(phones);
			for (PhoneNumber phoneNumber : phones) 
				phoneNumber.setContact(entreprise);
		}
		
		entreprise.setNumSiret(numSiret);
		
		return entreprise;
	}
	
	public PhoneNumber newPhoneNumber(String phoneKind, String phoneNumber, Contact contact) {
		PhoneNumber phone = new PhoneNumber();
		phone.setPhoneKind(phoneKind);
		phone.setPhoneNumber(phoneNumber);
		phone.setContact(contact);
		
		if (contact != null)
			contact.getPhones().add(phone);
		
		return phone;
	}
}
